import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'

// 替换为 Rollup 兼容插件
import obfuscator from 'rollup-plugin-javascript-obfuscator'
const encryption = true; // 打包后的代码是否加密

export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
  ],
  server:{
    host:"0.0.0.0",
    port:7150,//项目端口
    open:false,//启动浏览器
    cors:true,//允许跨域
    proxy: {
      // 在此处编写代理规则
      '/tp': {
        target: 'http://localhost:7155',
        changeOrigin: true,
        rewrite: (path) => {
          return path.replace(/\/tp/, '')
        }
      }
    }
  },
  //混淆设置
  build: {
    rollupOptions: {
      plugins: [
        // 使用 rollup-plugin-javascript-obfuscator
        encryption && obfuscator({
          rotateStringArray: true,
          stringArray: true,
          stringArrayEncoding: ['base64'],
          stringArrayThreshold: 0.75,
          identifierNamesGenerator: 'hexadecimal',
          renameGlobals: false,
          controlFlowFlattening: true,
          controlFlowFlatteningThreshold: 0.75,
          deadCodeInjection: true
        })
      ].filter(Boolean) // 过滤掉 false 值
    }
  },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
  }
})
